package cn.edu.sdu.sol.intake_system.service;

import cn.edu.sdu.sol.intake_system.model.po.*;
import cn.edu.sdu.sol.intake_system.model.vo.DataRequest;
import cn.edu.sdu.sol.intake_system.model.vo.DataResponse;
import cn.edu.sdu.sol.intake_system.repository.*;
import cn.edu.sdu.sol.intake_system.utils.JwtUtil;
import cn.edu.sdu.sol.intake_system.utils.PersonInfUtils;
import cn.edu.sdu.sol.intake_system.utils.VO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class AdminService {

    @Autowired
    JwtUtil jwtUtil;

    @Autowired
    UserRepository userRepository;

    @Autowired
    DepartmentRepository departmentRepository;

    VO vo = new VO();

    @Autowired
    PersonInfUtils personInfUtils;
    @Autowired
    private QuestionRepository questionRepository;
    @Autowired
    private AdminRepository adminRepository;
    @Autowired
    private StationRepository stationRepository;
    @Autowired
    private WillRepository willRepository;
    @Autowired
    private ScoreRepository scoreRepository;
    @Autowired
    private AnswerRepository answerRepository;

    public DataResponse update(DataRequest req, String token) {
        //获取用户信息
        token = jwtUtil.extractTokenFromHeader(token);
        String studentNum = jwtUtil.getStudentNumFromToken(token);
        Optional<SysUser> sysUser = userRepository.findUserByStudentNum(studentNum);
        assert sysUser.isPresent();
        SysUser s = sysUser.get();
        AdminInf a = s.getAdminInf();
        String school = req.getString("school");
        String position = req.getString("position");
        a.setPosition(position);
        a.setSchool(school);
        adminRepository.saveAndFlush(a);
        userRepository.saveAndFlush(s);
        return new DataResponse(200, null, "修改成功~");
    }

    public DataResponse updateDepartment(DataRequest req, String token) {
        //获取用户信息
        token = jwtUtil.extractTokenFromHeader(token);
        String studentNum = jwtUtil.getStudentNumFromToken(token);
        Optional<SysUser> sysUser = userRepository.findUserByStudentNum(studentNum);
        assert sysUser.isPresent();
        Integer departmentId = req.getInteger("departmentId");
        String penTime = req.getString("penTime");
        String faceTime = req.getString("faceTime");
        String introduction = req.getString("introduction");
        Integer stationId = req.getInteger("stationId");
        Department d = departmentRepository.findDepartmentByDepartmentId(departmentId).get();
        d.setIntroduction(introduction);
        d.setPenTime(penTime);
        d.setFaceTime(faceTime);
        Optional<Station> s0 = stationRepository.findStationByStationId(stationId);
        if (s0.isPresent()) {
            Station s = s0.get();
            d.setStation(s);
        }
        departmentRepository.saveAndFlush(d);
        return new DataResponse(200, null, "修改成功~");
    }

    public DataResponse getDepartmentInf(DataRequest req, String token) {
        Integer departmentId = req.getInteger("departmentId");
        Department d = departmentRepository.findDepartmentByDepartmentId(departmentId).get();
        return new DataResponse(200, d, "ok");

    }


    public DataResponse getPersonInf(DataRequest req, String token) {
        //获取用户信息
        token = jwtUtil.extractTokenFromHeader(token);
        String studentNum = jwtUtil.getStudentNumFromToken(token);
        Integer personId = req.getInteger("personId");
        Integer departmentId = req.getInteger("departmentId");
        Optional<Will> will = willRepository.findWillByPersonInfPkAAndDepartmentId(personId, departmentId);
        if (will.isEmpty()) {
            return vo.messageError("该用户无该部门报名信息");
        }
        Will w = will.get();
        Optional<SysUser> sysUser = userRepository.findUserByPersonId(personId);
        assert sysUser.isPresent();
        SysUser s = sysUser.get();
        Map data = new HashMap();
        String gender;
        if(s.getPersonInf().getGender() == Gender.MALE){
            gender = "男";
        } else if(s.getPersonInf().getGender() == Gender.FEMALE){
            gender = "女";
        } else {
            gender = "未知";
        }
        data.put("studentNum", s.getStudentNum());
        data.put("name", s.getName());
        data.put("gender", gender);
        data.put("birthday", s.getPersonInf().getBirthday());
        data.put("academy", s.getPersonInf().getAcademy());
        data.put("profession", s.getPersonInf().getProfession());
        data.put("email", s.getPersonInf().getEmail());
        data.put("phoneNum", s.getPersonInf().getPhoneNum());
        data.put("qqNum", s.getPersonInf().getQqNum());
        data.put("birthPlace", s.getPersonInf().getBirthPlace());
        data.put("introduction",s.getPersonInf().getIntroduction());
        data.put("interviewRecord",w.getScore().getInterviewRecord());
        data.put("headImg", s.getHeadImg());
        data.put("school", s.getPersonInf().getSchool());
        List dataList = new ArrayList();
        List<Answer> qList = answerRepository.findAnswerListByDepartmentIdAAndPersonId(departmentId, personId);
        for (int i = 0; i < qList.size(); i++) {
            Answer answer = qList.get(i);
            Map m = new HashMap();
            if (answer == null) {
                dataList.add(m);
            } else {
                m.put("answerId", answer.getAnswerId());
                m.put("questionId", answer.getQuestion().getQuestionId());
                m.put("question", answer.getQuestion().getStem());
                m.put("answer", answer.getAnswer());
                dataList.add(m);
            }
        }
        data.put("answers", dataList);
        return new DataResponse(200, data, "ok");
    }

    public DataResponse getPassedPerson(DataRequest req, String token) {
        //获取用户信息
        token = jwtUtil.extractTokenFromHeader(token);
        String studentNum = jwtUtil.getStudentNumFromToken(token);
        Optional<SysUser> sysUser = userRepository.findUserByStudentNum(studentNum);
        assert sysUser.isPresent();
        SysUser s = sysUser.get();
        Integer departmentId = req.getInteger("departmentId");
        List<Will> qList = new ArrayList<Will>();
        if(departmentId==0){
             qList = willRepository.findAllPassed();
        }else{
            qList = willRepository.findPassedWillListByDepartmentId(departmentId);
        }
        List dataList = new ArrayList();
        if (qList == null || qList.size() == 0) {
            return new DataResponse(200, dataList, "无志愿信息");
        }
        for (int i = 0; i < qList.size(); i++) {
            Will will = qList.get(i);
            Map m = new HashMap();
            if (will == null) {
                dataList.add(m);
            } else {
                m.put("name", will.getPerson().getName());
                m.put("willOrder", will.getWillOrder());
                m.put("departmentName", will.getDepartment().getName());
                m.put("gender", will.getPerson().getGender());
                m.put("academy", will.getPerson().getAcademy());
                m.put("profession", will.getPerson().getProfession());
                m.put("email", will.getPerson().getEmail());
                m.put("phoneNum", will.getPerson().getPhoneNum());
                m.put("qqNum", will.getPerson().getQqNum());
                m.put("passTime",will.getScore().getPassTime());
                m.put("station",will.getDepartment().getStation().getName());
                dataList.add(m);
            }
        }
        return new DataResponse(200,dataList,"ok");
    }

    public DataResponse getAdminInf(String token) {
        //获取用户信息
        token = jwtUtil.extractTokenFromHeader(token);
        String studentNum = jwtUtil.getStudentNumFromToken(token);
        Optional<SysUser> sysUser = userRepository.findUserByStudentNum(studentNum);
        assert sysUser.isPresent();
        SysUser s = sysUser.get();
        AdminInf adminInf = s.getAdminInf();
        Map data = new HashMap();
        data.put("name", s.getName());
        data.put("school", adminInf.getSchool());
        data.put("department", adminInf.getDepartment());
        data.put("position", adminInf.getPosition());
        data.put("headImg", s.getHeadImg());
        return new DataResponse(200, data, "ok");
    }
}
      

